var pageSize = 10;
var deptTypeMap = {};
$(document).ready(function(){
	commonSet();
	//加载查询条件列表
	initQueryConditionHos();
	//表格初始化
	initTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
	//科室类型
	deptTypeMap = getTransParam('deptTypeInfo');
}

//事件绑定
function eventBind(){
	//带有分页信息的查询
	$("#queryBtn").click(pageQuery).click();
	
	//添加按钮弹出层
	$("#addBtn").click(showAddFormDialog);
}
//加载查询条件列表
function initQueryConditionHos(){
	$.getApiClient('/baseInfo/hospitals').get().done(function(data){
		if(!data.payload){
			data.payload=[];
		}
	    multipleSelectData("hospital",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), null, {valueAttr:'id', displayAttr:'hospital_name'},"请选择");
		$('#hospital').multipleSelect({width:'100%', single: true});
	});
}

//表格初始化
function initTable(){
	$('#deptListTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			query(number, size);
		}
	});
}

//带有分页信息的查询
function pageQuery(){
	query(1, pageSize);
};

//分页查询
function query(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#queryForm").collect()
	}
	
	$.getApiClient('/baseInfo/departmentsPager').setQuery(queryCondition).get().done(function(data){
	    $('#deptListTable').bootstrapTable('load', data.payload);
	});
};

//添加弹出
function showAddFormDialog(){
	customRemoteUrlDialog('添加科室','/page/business/sysmanage/department/form?type=add',null);
}

//修改弹出
function showEditFormDialog(row){
	customRemoteUrlDialog('修改科室','/page/business/sysmanage/department/form?type=edit&deptId='+row.id,null);
}

//删除科室弹出
function showDelFormDialog(row){
	customConfirmDialog('是否确认删除科室？','删除科室',null,function(dialog){
		//执行删除
		dialog.close();
		doDeleteDept(row.id,function(data){
			if(data.code == "0"){
				customTimeOutDialog('信息提示','删除科室成功',null,2000);
				pageQuery();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		})
	});
	
	function doDeleteDept(deptId,callBack){
		$.getApiClient("/baseInfo/department/{id}").setQuery({
			id : deptId
		}).del().done(function(data){
			callBack(data)
		});
	}
}

//操作列
function operateFormatter(value, row, index) {
	return [
	        '<a class="edit" href="javascript:void(0)" title="修改"><i class="fa fa-edit"></i>修改</a>',
	        '&nbsp;&nbsp;&nbsp;',
	        '<a class="delete" href="javascript:void(0)" title="删除"><i class="fa fa-close"></i>删除</a>'
	    ].join('');
};

//表格数据绑定
operateEvents = {
	'click .edit' : function(e, value, row, index) {
		showEditFormDialog(row);
	},
	'click .delete' : function(e, value, row, index) {
		showDelFormDialog(row);
	}
};